package com.example.user.view;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.common.user.GlobalUser;
import com.example.common.user.view.LoginMainActivity;
import com.example.user.R;
import com.example.user.UserPageCollectionAdapter;
import com.example.user.databinding.UserMyProfileDetailFragmentBinding;
import com.example.user.viewmodel.UserMyProfileDetailViewModel;
import com.google.android.material.tabs.TabLayoutMediator;

public class UserMyProfileDetailFragment extends Fragment {

    private static final String TAG = "UserMyProfileDetailFragment";
    private UserMyProfileDetailViewModel mViewModel;
    UserPageCollectionAdapter userPageCollectionAdapter;
    private UserMyProfileDetailFragmentBinding binding;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        binding = DataBindingUtil.inflate(inflater,
                R.layout.user_my_profile_detail_fragment, container,
                false);

        mViewModel = new ViewModelProvider(requireActivity()).get(UserMyProfileDetailViewModel.class);

        //数据绑定
        binding.setData(mViewModel.getUserDetail());
        binding.setGlobalUser(GlobalUser.getInstance());
        //生命周期绑定
        binding.setLifecycleOwner(requireActivity());

        //按钮显示监听
        mViewModel.getIsNeedToLogin().observe(requireActivity(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    //todo 显示点击登录
                    binding.userMyprofileModifyInfoButton.setVisibility(View.GONE);
                    binding.userDetailBtClickLogin.setVisibility(View.VISIBLE);
                } else {
                    //todo 显示修改资料
                    binding.userMyprofileModifyInfoButton.setVisibility(View.VISIBLE);
                    binding.userDetailBtClickLogin.setVisibility(View.GONE);
                }
            }
        });
        GlobalUser.getInstance().getBindIsLogin().observe(requireActivity(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    //todo 显示点击登录
                    binding.userMyprofileModifyInfoButton.setVisibility(View.GONE);
                    binding.userDetailBtClickLogin.setVisibility(View.VISIBLE);
                } else {
                    //todo 显示修改资料
                    binding.userMyprofileModifyInfoButton.setVisibility(View.VISIBLE);
                    binding.userDetailBtClickLogin.setVisibility(View.GONE);
                }
            }
        });

        //tabview的adapter
        userPageCollectionAdapter = new UserPageCollectionAdapter(this);
        binding.userViewPager.setAdapter(userPageCollectionAdapter);


        //点击——跳转信息修改页面
        binding.userMyprofileModifyInfoButton.setOnClickListener(v -> {
            //导航

            NavController navController = Navigation.findNavController(requireView());
            navController.navigate(R.id.action_userMyProfileDetailFragment_to_userModifyInfoFragment2,
                    mViewModel.translateBundle());
        });

        //点击跳转登录
        binding.userDetailBtClickLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(requireActivity(), LoginMainActivity.class);
                startActivity(intent);
            }
        });


        //tabview当前页面第一页
        binding.userViewPager.setCurrentItem(0);

        new TabLayoutMediator(binding.userTabLayout, binding.userViewPager,
                (tab, position) -> tab.setText(userPageCollectionAdapter.getFragmentName(position))).attach();

        View view = binding.getRoot();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // TODO: Use the ViewModel
        if (getArguments() != null) {
            Bundle bundle = getArguments();
            boolean change = bundle.getBoolean("CHANGE");
            if (change) {
                mViewModel.getUserDetailOnline();
            }
        }

        if (GlobalUser.IS_LOGIN) {
            mViewModel.getUserDetailOnline();
            mViewModel.getIsNeedToLogin().setValue(false);
        } else {
            mViewModel.getIsNeedToLogin().setValue(true);
        }
    }

}